<template>
	<view :class="show?'ercode':'active'">
		
		<view id="poster">
			<image class="bac" src="https://cos.zaituan.com.cn/uploads/images/original/20250828/76594a3d28f9a805177ca489b5426db8.png"></image>
			<view class="main_body">
				<view class="main" id="qrcode-view">
					
					<!-- <view class="qrcode_imag" style="width: 100%;overflow: hidden;"></view> -->
					<image :src="qrcode" mode="" style="width: 460rpx;height: 460rpx;display: block;margin: 0 auto;"></image>
					<view class="shop-name" v-if="message && message.store">
						<text style="width: 300rpx;display: block;margin: 0 auto;font-size: 34rpx;text-align: center;
						overflow:hidden;text-overflow:ellipsis;white-space:nowrap">{{message.store.name}}</text>
					</view>
				</view>
			</view>	
		</view>

		<view class="btn" @click="capture" v-if="show">
			获取完整图片
		</view>	
		<view class="btn" @click="download" v-if="show" style="background: none;border: 1rpx solid #FF7104;color: #000;
		width: 450rpx;height: 70rpx;border-radius: 40rpx;text-align: center;line-height: 70rpx;margin: 50rpx auto 120rpx;">
			获取二维码图片
		</view>	

		<view class="goods-qrcode-modal" v-if="showPoster">
			<view class="goods-qrcode-body flex-col">
				<!-- 整一个图片包括二维码都是后台给的图片 -->
				<view class="goods-qrcode2 flex flex-y-center flex-x-center">
					<view class="codeImg_box">
						<view class="goods-qrcode-box">
							<image :src="poster_url" class="goods-qrcode" mode='widthFix'></image>
						</view>
					</view>
					<view class="saveCode-btn">长按图片保存至本地</view>
				</view>
				
				<view class="goods-qrcode-close" @click="closePost">
					<view style="width: 50rpx;height: 50rpx;text-align: center;background-color: #ADADAD;color: #FFFFFF;border-radius: 50%;line-height: 50rpx;"
					 class="iconfont icon-guanbi"></view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import QRCode from 'qrcode';
	import { snapdom } from '@zumer/snapdom';
	import html2canvas from 'html2canvas'
	export default {
		data() {
			return {
				qrcode:'',
				message: {},
				showPoster: false,
				poster_url:"",
				show:true,
			}
		},
		async onLoad(opt) {
			let mch_id = uni.getStorageSync('mch_id');
			let userInfo = uni.getStorageSync('userInfo');
			userInfo = userInfo ? JSON.parse(userInfo) : {};
			let cashier = opt.cashier ?? 0;
			let url = window.location.protocol + '//' + window.location.host + window.location.pathname + '#/mch/personalCentre/ercode/payPages/payPages?id='+mch_id+'&pid='+userInfo.user_id+'&cashier='+cashier;
			
			const options = {
				width: 300,  // 二维码的宽度
				height: 300, // 二维码的高度
			};
			this.qrcode = await QRCode.toDataURL(url,options);

			if (uni.getStorageSync('mchMessage')) {
				this.message = uni.getStorageSync('mchMessage')
			}
		},
		methods: {
			async capture() {
				
				window.pageYoffset = 0;
				document.documentElement.scrollTop = 0;
				document.body.scrollTop = 0;
				const canvas = await html2canvas(document.getElementById('poster'), { 
					width: document.getElementById('poster').clientWidth,
					height: document.getElementById('poster').clientHeight,
					scale:3,
					dpi: window.devicePixelRatio * 3,
					useCORS: true,
				});
				this.poster_url = canvas.toDataURL('image/png', 1);
				this.showPoster = true;
				
				
				
				// const poster = document.getElementById('poster');
				// this.poster_url = await snapdom.toRaw(poster);
				// this.showPoster=true
			},
			closePost(){
				this.showPoster = false;
				this.show=true
			},
			download(){ //下载图片
				this.poster_url = this.qrcode;
				this.showPoster=true;
			},

		}
	}
</script>

<style scoped lang="less">
	.ercode {
		width: 100%;
		height: 100%;
		position: relative;
	}
	.active{
		width: 100%;
		height: 90%;
		position: relative;
	}
	.bac{width: 100%;height: 1100rpx;display: block;}
	.main_body{width: 100%;overflow: hidden;position: absolute;top: 320rpx;z-index: 999;left: 0;}
	.main{width: 100%;}
	/* #ifdef H5 ||APP-PLUS*/
.shop-name{width: 100%;overflow: hidden;text-align: center;color: #000 ;font-weight: bold;font-size: 36rpx;overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;margin-top: 60rpx;}
	/* #endif */
	/* #ifdef  MP */
	.shop-name{width: 100%;overflow: hidden;text-align: center;color: #000 ;font-weight: bold;font-size: 36rpx;overflow: hidden;
	text-overflow:ellipsis;
	white-space: nowrap;margin-bottom: 48rpx;}
	/* #endif */
	.notice{width: 100%;overflow: hidden;text-align: center;color: #fff;font-weight: bold;font-size: 36rpx;margin: 40rpx 0 0 0;}
	.btn {
		width: 450rpx;
		background: #FF7104;
		height: 70rpx;
		border-radius: 40rpx;
		text-align: center;
		color: #fff;
		line-height: 70rpx;
		margin: 60rpx auto 0rpx;
	}
	.goods-qrcode-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1000;
		background: rgba(0, 0, 0, 0.5);
		opacity: 1;
		transition: opacity 250ms;
		z-index: 9999;
	}
	
	.goods-qrcode-body {
		background: #ffffff;
		height: 100%;
		border-radius: 10rpx;
		padding: 30rpx;
	}
	
	.goods-qrcode-body .goods-qrcode-box {
		display: flex;
		box-shadow: 0 0 15rpx rgba(0, 0, 0, 0.15);
	}
	.goods-qrcode2{
		position: relative;
		height: 1300rpx;
		flex-direction: column;
		margin-top: 40rpx;
	}
	.codeImg_box {
		width: 92%;
		margin-bottom: 20rpx;
	}
	.saveCode-btn{
		color: #939292;
		padding: 10px 20px;
		border-radius: 10px;
	}
	
	.goods-qrcode {
		width: 100%;
		height: 100%;
		background: #ffffff;
		background-size: cover;
	}
	
	.goods-qrcode-close {
		position: absolute;
		top: 40rpx;
		/* #ifdef MP-WEIXIN ||APP-PLUS */
		top: 150rpx;
		/* #endif */
		right: 40rpx;
		padding: 15rpx;
	}
	
	
	
	
	
	
	
	.cover{
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		background: #fff;
	}
	.cover view{
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		height: 600rpx;
		margin: auto;
		width: 500rpx;
	}
	.cover image{width: 320rpx;display: block;height: 350rpx;margin: 0 auto;}
</style>

